var clickState=true;
$(document).ready(function(){
	$(document).on("click", ".j_delete_pictures", function() {
	      $(this).parents(".j_picture_item:first").remove();
	 });
	$("#close-button").click(function(){
		var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
	});
	var editor = KindEditor.create('textarea[name="remark"]',kind_editor_option);
  layui.use(['upload'],function(){
  layui.upload({
    url: basePath+'/restful/file/upload.shtml', //上传接口
    ext: 'jpg|png|bmp|jpeg',
    method: 'post',
    success: function(res,input){ //上传成功后的回调
    	console.log(res);
    	var type=$(input).attr("data-type");
      if(res.code=="000000"){
          var id = res.data[0].id;
	      var src=imgUrl+id;
	      if(type == "more"){
	    	 var html="<li><img data-img=\""+id+"\"  src=\""+src+"\"><a class=\"closeImage-modal\" onclick=\"deleteImage(this)\">x</a></li>";
	    	 $(".goods-image").append(html);
		   }else{
		     $(".one-image").attr("src",src);
		     $(".one-image").attr("data-img",id);
		   }
      }else{
      	layer.msg("上传失败")
      }
    }
  });
});
});
function deleteli(e){$(e).parent().parent().remove();}
function deleteTr(e){$(e).parent().parent().remove();}
function deleteImage(e){$(e).parent().remove();}
function addColorSize(){
	var content="";
		content += '<tr class=\"color-tr\">';
		content += 	 '<td>';
		content += 		'<input class="am-input-sm" name="itemId" type="hidden" value=""/>';
		content += 		'<input class="am-input-sm" name="colorName" type="text" value=""/>';
		content += 	'</td>';
	    content += 	 '<td>';
	    content += 		'<input class="am-input-sm" name="colorCode" type="text" value=""/>';
	    content += 	'</td>';
	    content += 	'<td>';
	    content += 		'<input class="am-input-sm" name="sizeName" type="text" value=""/>';
	    content += 	'</td>';
	    content += 	'<td>';
	    content += 		'<input class="am-input-sm" name="sizeCode" type="text" value=""/>';
	    content += 	'</td>';
	    content += 	'<td>';
	    content += 		'<input class="am-input-sm" name="sku" type="text" value=""/>';
	    content += 	'</td>';
	    content += 	'<td>';
	    content += 		'<input class="am-input-sm" name="stockedQuantity" type="text" value=""/>';
	    content += 	'</td>';
	    content += 	'<td>';
	    content += 		'<button class="btn btn-default j_delete_sizes" onclick ="deleteTr(this)">';
	    content +=			'<span class="am-icon-trash-o"></span> 删除';
	    content +=		'</button>';
	    content += 	'</td>';
	    content += '</tr>';
		$("#color_table").append(content);
}

function saveGoods(){
	var data={};
	var goodsId=$("input[name='goodsId']").val();
	var goodsName=$("input[name='goodsName']").val();
	var styleCode=$("input[name='styleCode']").val();
	var goodsTags=$("input[name='goodsTags']").val();
	var smallDesc=$("input[name='smallDesc']").val();
	var material=$("input[name='material']").val();
	var price=$("input[name='price']").val();
	var tags=$("input[name='goodsTags']").val();
	var content=$("#remark").val();
	var typeId=$("select[name='selectGoodsType']").val();
	var brandId=$("select[name='selBrand']").val();
	var quarter=$("select[name='selQuarter']").val();
	var isSp=$("input[name='isSp']:checked").val();
	var status=$("input[name='status']:checked").val();
	var minimum = $("#minimumQuantity").val();
	if(minimum.length==0){
		minimum=1;
	}
	if(goodsName.length==0){
		layer.msg("商品名称不能为空");
		return false;
	}
	if(status.length==0){
		layer.msg("请选择商品状态!");
		return false;
	}
	if(styleCode.length==0){
		layer.msg("商品款号不能为空");
		return false;
	}
	/*if(smallDesc.length==0){
		layer.msg("商品简介不能为空");
		return false;
	}
	if(material.length==0){
		layer.msg("商品材质不能为空");
		return false;
	}*/
	if(quarter.length==0){
		layer.msg("请选择季度");
		return false;
	}
	if(content.length==0){
		layer.msg("商品详细描述内容不能为空!");
		return false;
	}
	/*if(tags==""){
		layer.msg("商品标签不能为空!");
		return false;
	}*/
	if(typeId==0){
		layer.msg("请选择商品类型");
		return false;
	}
	if(brandId==0){
		layer.msg("请选择品牌");
		return false;
	}
	if(price==""){
		layer.msg("请输入商品价格！");
		return false;
	}
	if(price<=0){
		layer.msg("请核对销售价！");
		return false;
	}
	data.styleCode=styleCode;
	data.goodsName=goodsName;
	data.typeId=typeId;
	data.brandId=brandId;
	data.tags=tags;
	data.goodsTags=goodsTags;
	data.price=price;
	data.isSp=isSp;
	data.smallDesc=smallDesc;
	data.material=material;
	data.content=content;
	data.quarter =quarter;
	data.goodsStatus =status;
	data.id =goodsId;
	data.minimumQuantity =minimum;
	//获取封面图信息
	var coverImage = $(".one-image").attr("data-img");
	data.coverImage = coverImage;
	//获取商品图片信息
	var imageList=new Array();
	var image_img=$(".am-form-group .goods-image li");
	for(var i=0;i<image_img.length;i++){
		var imgId = $(image_img[i]).children("img").attr("data-img");
		if(imgId.length>1){
			var imgObject={
					imageUrl:imgId
			}
			imageList.push(imgObject);
		}
	}
	if(imageList.length==0){
		layer.msg("请添加商品图片");
		return false;
	}
	data.imageList=imageList;
	//
	var sizeInfo = $("#color_table .color-tr");
	var sizeList = new Array();
	for (var i=0;i<sizeInfo.length;i++) {
		var itemId = $(sizeInfo[i]).find("input[name='itemId']").val();
		var colorName = $(sizeInfo[i]).find("input[name='colorName']").val();
		var colorCode = $(sizeInfo[i]).find("input[name='colorCode']").val();
		var sizeCode = $(sizeInfo[i]).find("input[name='sizeCode']").val();
		var sizeName = $(sizeInfo[i]).find("input[name='sizeName']").val();
		var sku = $(sizeInfo[i]).find("input[name='sku']").val();
		var stockedQuantity = $(sizeInfo[i]).find("input[name='stockedQuantity']").val();
		if(colorName.length<1){
			layer.msg("请输入颜色名称");
			return false;
		}
		if(colorCode.length<1){
			layer.msg("请输入颜色码");
			return false;
		}
		if(sizeCode.length<1){
			layer.msg("请输入尺码");
			return false;
		}
		if(sizeName.length<1){
			layer.msg("请输入尺码名称");
			return false;
		}
		if(sku.length<1){
			layer.msg("请输入SKU");
			return false;
		}
		if(stockedQuantity.length<1){
			layer.msg("请输入库存数量");
			return false;
		}
		var sizeObject={
				id:itemId,
				colorCode:colorCode,
				colorName:colorName,
				sizeCode:sizeCode,
				sizeName:sizeName,
				sku:sku,
				stockedQuantity:stockedQuantity
		}
		sizeList.push(sizeObject);
	}
	if(sizeList.length==0){
		layer.msg("请添加颜色尺码信息");
		return false;
	}
	data.itemList = sizeList;
	if(clickState){
		clickState=false;
		$.ajax({
			url:basePath+"/goods/save.shtml",  
			type:"post",
			data:{goodsDetail:JSON.stringify(data)},
			dataType:"JSON",
			success:function(data){
				layer.msg(data.msg);
				if(data.code=="000000"){
					setTimeout(function(){    
						parent.closeLayer();
					}, 1000);
				}else{
					clickState=true;
				}
			},error:function(data){
			}
		});
	}
}
